本次学习内容:Spring 依赖注入相关
xml 配置实现
setXXX() 方法注入
1 2 3
| <bean id="book" class="com.geng.attr.Book"> <property name="bookName" value="西游记"/> </bean>
|
构造方法注入
1 2 3 4
| // 这里通过有参构造注入 <bean id="people" class="com.geng.attr.People"> <constructor-arg name="name" value="小明"/> </bean>
|
对象注入
1 2 3 4
| <bean id="userDao" class="com.geng.obj.UserDao"/> <bean id="userService" class="com.geng.obj.UserService"> <property name="userDao" ref="userDao"/> </bean>
|
1 2 3 4 5 6
| public class UserDao { public void showUserDao() { System.out.println("show user dao..."); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void showService() { System.out.println("show Service...."); userDao.showUserDao(); } }
|
测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class UserServiceTest { private ApplicationContext context; @Before public void setUp() throws Exception { context = new ClassPathXmlApplicationContext("beans1.xml"); } @Test public void showService() throws Exception { UserService userService = (UserService) context.getBean("userService"); userService.showService(); } }
|
p命名空间注入
xml 头部加入:
1
| xmlns:p="http://www.springframework.org/schema/p"
|
配置:
1
| <bean id="person" class="com.geng.attr.Person" p:pName="呵呵哒”/>
|
1 2 3 4 5 6 7 8 9 10 11 12
| public class Person { private String pName; public void setpName(String pName) { this.pName = pName; } public void test() { System.out.println("result: " + pName); } }
|
复杂类型注入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class MulitDemo { private String[] arrs; private List<String> list; private Map<String, String> map; private Properties properties; public void setArrs(String[] arrs) { this.arrs = arrs; } public void setList(List<String> list) { this.list = list; } public void setMap(Map<String, String> map) { this.map = map; } public void setProperties(Properties properties) { this.properties = properties; } public void showAll() { System.out.println(arrs); System.out.println(list); System.out.println(map); System.out.println(properties); } }
|
xml 分别配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <bean id="mulitDemo" class="com.geng.collection.MulitDemo"> <property name="arrs"> <array> <value>小红</value> <value>小白</value> <value>小绿</value> </array> </property> <property name="list"> <list> <value>大白</value> <value>大春</value> <value>大花</value> </list> </property> <property name="map"> <map> <entry key="001" value="小白" /> <entry key="002" value="小刘" /> <entry key="003" value="小东" /> </map> </property> <property name="properties"> <props> <prop key="driveClass">com.mysql.jdbc.Driver</prop> <prop key="userName">root</prop> <prop key="userPwd">root</prop> </props> </property> </bean>
|
测试:
注解实现
1 2 3 4 5 6 7 8 9 10
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> </beans>
|
创建对象注解
- @Component
- @Controller —— web 层
- @Service —— 业务层
- @Repository —— 持久层
以上4个注解 功能是一致的,都是用于创建对象,作用在 类 上,只是为了后期方便扩展
对象的 scope
1
| @Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
可查看源码进行设置
对象属性注入
- @Autowired:自动装配
- @Resource(name=“userDao”) 指定创建哪种类型的对象
1 2 3 4 5 6 7
| @Component(value = "userDao") public class UserDao { public void showUserDao() { System.out.println("show user dao ..."); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @Service(value = "userService") public class UserService { @Resource(name = "userDao") private UserDao userDao; public void showUserService() { System.out.println("show user service ..."); userDao.showUserDao(); } }
|
测试:
xml 配置注入 和 注解注入 两种可以联合使用,用法和前面一样,不再举例。
IOC 与 DI 区别
IOC :控制反转,将创建对象交给 Spring 的配置来完成
DI :将对象的属性赋值
DI 是依赖于 ioc 才能完成操作,不能单独存在
附录:
demo地址:
https://github.com/fenglincanyi/springdemo1
https://github.com/fenglincanyi/springdemo2